//
//  LXCalenderCell.m
//  LXCalendar
//
//  Created by chenergou on 2017/11/2.
//  Copyright © 2017年 漫漫. All rights reserved.
//

#import "LXCalenderCell.h"
@interface LXCalenderCell()
@property (weak, nonatomic) IBOutlet UILabel *label;

@property (strong, nonatomic)NSString *receivable;
@property (strong, nonatomic)NSString *hadReceivable;

@end
@implementation LXCalenderCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _hadReceivable = @"";
    _receivable = @"";

}

-(void)setData:(NSArray *)data{
    
    _data = data;
//    NSLog(@"---------%@",self.data);
    
}

-(void)setModel:(LXCalendarDayModel *)model{
    _model = model;
    
    self.label.text = [NSString stringWithFormat:@"%ld",(long)model.day];
    self.label.layer.cornerRadius = self.label.lx_width/2;
    self.label.layer.masksToBounds = YES;
    self.label.backgroundColor = [UIColor whiteColor];
    
    NSString *times = [NSString stringWithFormat:@"%ld-%02ld-%02ld",(long)model.year,(long)model.month,(long)model.day];
    
  
    for (NSDictionary *obj in _data) {
        
        if ( [obj[@"time"] isEqualToString:times]&&[obj[@"has_repay"] integerValue] == 1) {//已回款
            
            _hadReceivable = obj[@"time"];
            
            
        }else if([obj[@"time"] isEqualToString:times]&&[obj[@"has_repay"] integerValue] == 0){//未回款
            
            _receivable = obj[@"time"];
            
        }
    }
    
    
    if (model.isNextMonth || model.isLastMonth) {
        self.userInteractionEnabled = NO;
        
        if (model.isShowLastAndNextDate) {
            
            self.label.hidden = NO;

        }else{

            self.label.hidden = YES;
        }
        
        [self reloadLable:times];

        
    }else{

        self.label.hidden = NO;
        self.userInteractionEnabled = YES;
        
        
        if (model.isSelected) {

            [self selectedDayLable:times];
            
            if (model.isHaveAnimation) {
                 [self addAnimaiton];
            }
           
        }else{
            
            [self reloadLable:times];

        }
    }
}


-(void)reloadLable:(NSString *)times{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置日期格式
    [formatter setDateFormat:@"yyyy-MM-dd"];
    // 获取当前日期
    NSDate *currentDate = [NSDate date];
    NSString *currentDateString = [formatter stringFromDate:currentDate];
    
    
    if ([currentDateString isEqualToString:times]) {
        
        self.label.backgroundColor = [UIColor colorForHex:@"#31C7FF"];
        self.label.layer.borderColor = [UIColor whiteColor].CGColor;
        self.label.layer.borderWidth= 0;
    
    }else if ([_hadReceivable isEqualToString:times]){
        
        self.label.backgroundColor = [UIColor whiteColor];
        self.label.layer.borderColor = [UIColor colorForHex:@"#FFBC04"].CGColor;
        self.label.layer.borderWidth= 1;
        
    }else if ([_receivable isEqualToString:times]){
        
        self.label.backgroundColor = [UIColor whiteColor];
        self.label.layer.borderColor = [UIColor colorForHex:@"#FE623C"].CGColor;
        self.label.layer.borderWidth= 1;
        
    }else{
        
        self.label.backgroundColor = [UIColor whiteColor];
        self.label.layer.borderColor = [UIColor whiteColor].CGColor;
        self.label.layer.borderWidth= 0;
        
    }

}

-(void)selectedDayLable:(NSString *)times{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置日期格式
    [formatter setDateFormat:@"yyyy-MM-dd"];
    // 获取当前日期
    NSDate *currentDate = [NSDate date];
    NSString *currentDateString = [formatter stringFromDate:currentDate];
    
    if ([currentDateString isEqualToString:times]) {
        
        self.label.backgroundColor = [UIColor colorForHex:@"#31C7FF"];
        self.label.layer.borderColor = [UIColor whiteColor].CGColor;
        self.label.layer.borderWidth= 0;
        
    }else if ([_hadReceivable isEqualToString:times]){
        
        self.label.backgroundColor = [UIColor colorForHex:@"#FFBC04"];
        self.label.layer.borderWidth= 0;
        
    }else if ([_receivable isEqualToString:times]){
        
        self.label.backgroundColor = [UIColor colorForHex:@"#FE623C"];
        self.label.layer.borderWidth= 0;
        
    }else{
        
        self.label.backgroundColor = [UIColor colorForHex:@"#31C7FF"];
        self.label.layer.borderColor = [UIColor whiteColor].CGColor;
        self.label.layer.borderWidth= 0;
        
    }

}


-(void)addAnimaiton{
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    
    
    anim.values = @[@0.6,@1.2,@1.0];
//    anim.fromValue = @0.6;
    anim.keyPath = @"transform.scale";  // transform.scale 表示长和宽都缩放
    anim.calculationMode = kCAAnimationPaced;
    anim.duration = 0.25;                // 设置动画执行时间
//    anim.repeatCount = MAXFLOAT;        // MAXFLOAT 表示动画执行次数为无限次
    
//    anim.autoreverses = YES;            // 控制动画反转 默认情况下动画从尺寸1到0的过程中是有动画的，但是从0到1的过程中是没有动画的，设置autoreverses属性可以让尺寸0到1也是有过程的
    
    [self.label.layer addAnimation:anim forKey:nil];
}
@end
